<template>
  <div class="receive-post">
    <Button type="primary" @click="handleClick">接收岗位</Button>
    <el-dialog :visible.sync="visible" title="工单接收人岗位" append-to-body :close-on-click-modal="false" class="receive-dialog" @open="handleOpen">
      <div v-if="isCreate" class="title-post">处理人岗位</div>
      <Post :value="value" @input="handleInput"></Post>
      <template v-if="isCreate">
        <div class="title-post">抄送人岗位</div>
        <Post :value="carbonCopy" @input="handleUpdateCarbonCopy"></Post>
      </template>
      <div class="receive-btns">
        <Button type="default" @click="handleCancel">取消</Button>
        <Button type="primary" @click="handleConfirm">确定</Button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { ccDict, getDataDictByParentId } from '@/api/dataDict'
import { handlerPostList, handlerUpdate, queryReceiver, carbonCopyHandlerPost, automaticHandlerPost } from '@/api/workOrder'
import Post from './Post'
export default {
  name: 'ReceivePost',
  components: {
    Post
  },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    carbonCopy: { // 工单创建时 抄送岗位和接收岗位一起
      type: Array,
      default: () => []
    },
    isCreate: {
      type: Boolean,
      default: false
    },
    workOrderId: {
      type: String,
      default: ''
    },
    isCarbonCopy: {
      type: Boolean,
      default: false
    },
    workOrderData: {
      type: Object,
      default: undefined
    }
  },
  data() {
    return {
      visible: false,
      RECIPIENT_TYPE: []
    }
  },
  computed: {
  },
  created() {
    // this.getDictData()
  },
  methods: {
    handleClick() {
      this.visible = true
    },
    getDictData() {
      ccDict('RECIPIENT_TYPE').then(async res => {
        const allPromise = {}
        for (const item of res.data) {
          allPromise[item.dictCode] = getDataDictByParentId(item.dictId).then(res => {
            item.children = res.data
            return res
          })
        }
        await Promise.all(Object.values(allPromise))
        this.RECIPIENT_TYPE = res.data
      })
    },
    handleCancel() {
      this.visible = false
    },
    handleInput(v) {
      this.$emit('input', v)
    },
    handleUpdateCarbonCopy(v) {
      this.$emit('update:carbonCopy', v)
    },
    handleOpen() {
      this.handleInput([])
      this.handleUpdateCarbonCopy([])
      if (this.workOrderId) {
        if (this.isCreate) { // 创建工单 接收岗位、抄送岗位一起设置
          this.getReceviePostListCarbonCopy()
          this.getReceviePostList()
        } else {
          this.isCarbonCopy ? this.getReceviePostListCarbonCopy() : this.getReceviePostList()
        }
      } else if (this.workOrderData !== undefined) {
        this.getAutomaticHandlerPost()
      }
    },
    handleConfirm() {
      if (!this.isCreate) {
        if (!(this.value instanceof Array) || this.value.length === 0) {
          this.$message.warning('请选择岗位')
          return Promise.reject('请选择岗位')
        }
      } else if ((!(this.value instanceof Array) || this.value.length === 0) && (!(this.carbonCopy instanceof Array) || this.carbonCopy.length === 0)) {
        this.$message.warning('请选择岗位')
        return Promise.reject('请选择岗位')
      }
      if (this.workOrderId) {
        return (this.isCarbonCopy ? carbonCopyHandlerPost : handlerUpdate)(this.workOrderId, this.value).then(res => {
          if (res.msg) {
            this.$message.warning(res.msg)
          } else {
            this.$message.success('操作成功!')
          }
          this.visible = false
        })
      }
      return new Promise((resovle, reject) => {
        const _resovle = () => {
          resovle()
          this.visible = false
        }
        const _reject = () => {
          reject()
        }
        if (this.isCreate) {
          this.$emit('on-confirm', this.value, this.carbonCopy, _resovle, _reject)
        } else {
          this.$emit('on-confirm', this.value, _resovle, _reject)
        }
      })
    },
    getReceviePostList() {
      return handlerPostList(this.workOrderId).then(res => {
        this.handleInput(res.data)
      })
    },
    getReceviePostListCarbonCopy() {
      return queryReceiver(this.workOrderId).then(res => {
        if (this.isCreate) {
          this.handleUpdateCarbonCopy(res)
        } else {
          this.handleInput(res)
        }
      })
    },
    getAutomaticHandlerPost() {
      const data = { ...this.workOrderData }
      delete data.bizType
      delete data.hasShop
      delete data.shopArea
      delete data.potentialType
      return automaticHandlerPost(data).then(res => {
        this.handleInput(res.data.post || [])
        this.handleUpdateCarbonCopy(res.data.copyPost || [])
      })
    }
  }
}
</script>
<style lang="scss">
.receive-post{

}
.receive-title{
  color: black !important;
}
.block{
  .title{
    color: #333;
    font-size: 16px;
    font-weight: 600;
    line-height: 20px;
  }
  .checkbox{
    padding: 10px;
  }
}
.receive-btns{
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 10px 0;
}
.title-post{
  font-size: 20px;
  color: #16bc83;
  font-weight: 600;
  padding: 10px 0;
  border-bottom: 1px solid #16bc83;
  margin-bottom: 10px;
}
</style>
